<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\repositories\users;

use app\common\dao\users\UsersOauthDao;
use app\common\repositories\BaseRepository;

/**
 * Class UsersOauthRepository
 *
 * @mixin UsersOauthDao
 */
class UsersOauthRepository extends BaseRepository
{
    public function __construct(UsersOauthDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 绑定用户
     * @param int $propertyId
     * @param int $userId
     * @param string $openId
     * @param string $oauthType
     * @return \app\common\dao\BaseDao|int|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-01-24 22:09
     */
    public function bindUser(int $propertyId, int $userId, string $openId, string $oauthType)
    {
        $info = $this->dao->getByOpenId($openId, $oauthType, $propertyId);
        if ($info) {
            return $this->dao->update($info['id'], [
                'user_id' => $userId
            ]);
        } else {
            return $this->dao->create([
                'property_id' => $propertyId,
                'user_id' => $userId,
                'oauth_type' => $oauthType,
                'openid' => $openId
            ]);
        }
    }
}